package game.block.trap;

import game.block.Locator;
import game.block.command.Command;
import game.block.command.RemoveCommand;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Fire extends TrapEntity {

    private ParticleEffect particles;
    private OrthographicCamera camera;
    private Vector3 firePos;
    private Tile secondTile;
    private Vector2 adj;
    private int direction;
    private boolean tileOccupied;

    public Fire(Vector2 pos, Tile tile, char dir, OrthographicCamera camera) {
        super(pos, tile, FIRE_TILE, TRAPTYPE.FIRE);
        this.camera = camera;
        tileOccupied = false;
        particles = new ParticleEffect();
        particles.load(Gdx.files.internal("data/fire"), Gdx.files.internal("data/art/"));
        adj = new Vector2(0, 0);
        Locator.getEffects().add(particles);
        ParticleEmitter emitter = Locator.getEffects().get(Locator.getEffects().size() - 1).getEmitters().get(0);
        switch (dir) {
        case 'r':
            direction = 0;
            emitter.getAngle().setLow(0);
            adj.set(TILE_SIZE / 8, TILE_SIZE / 2);
            break;
        case 'u':
            direction = 90;
            emitter.getAngle().setLow(90);
            adj.set(TILE_SIZE / 2, TILE_SIZE / 8);
            break;
        case 'd':
            direction = -90;
            emitter.getAngle().setLow(-90);
            adj.set(TILE_SIZE / 2, TILE_SIZE - TILE_SIZE / 8);
            break;
        case 'l':
            direction = 180;
            emitter.getAngle().setLow(180);
            adj.set(TILE_SIZE - TILE_SIZE / 8, TILE_SIZE / 2);
            break;

        }
        firePos = new Vector3(tile.getPosition().x + adj.x, tile.getPosition().y + adj.y, 0);
        camera.project(firePos);
        particles.setPosition(firePos.x, firePos.y);
        particles.start();
    }

    public void initSecondTile(ArrayList<Tile> tileList, Board board) {
        int cPos = (int) (currentTile.getPosition().x / TILE_SIZE);
        int rPos = (int) (currentTile.getPosition().y / TILE_SIZE);
        int arrayPos = (board.getCols() - 1 - rPos) * board.getRows() + cPos;
        switch (direction) {
        case 0: // right
            secondTile = tileList.get(arrayPos + 1);
            break;
        case 90: // up
            secondTile = tileList.get(arrayPos - board.getRows());
            break;
        case -90: // down
            secondTile = tileList.get(arrayPos + board.getRows());
            break;
        case 180: // left
            secondTile = tileList.get(arrayPos - 1);
            break;
        }
        secondTile.add(this);
        secondTile.setSprite(this.sprite);
    }

    @Override
    public void render(SpriteBatch sb) {
        super.render(sb);
    }

    @Override
    public void update(float delta) {
        super.update(delta);
        firePos = new Vector3(currentTile.getPosition().x + adj.x, currentTile.getPosition().y + adj.y, 0);
        camera.project(firePos);
        particles.setPosition(firePos.x, firePos.y);
        if (Locator.getEffects().size() > 0) {
            ParticleEmitter emitter = Locator.getEffects().get(Locator.getEffects().size() - 1).getEmitters().get(0);
            emitter.getScale().setHigh(1 / camera.zoom * 32);
        }

        if (!tileOccupied && currentTile.containsStone()) {
            Locator.getEffects().remove(particles);
            tileOccupied = true;
        } else if (tileOccupied && !currentTile.containsStone()) {
            Locator.getEffects().add(particles);
            tileOccupied = false;
        }
        
/*        if(secondTile.containsStone()){
        	ParticleEmitter emitter = Locator.getEffects().get(Locator.getEffects().size() - 1).getEmitters().get(0);
            //emitter.getScale().setHigh(100);
        }*/
        
    }

    @Override
    public Command advance() {
        return null;
    }

    @Override
    public void applyEffect(MovableEntity block, Board gameBoard, Turn currentTurn, Tile tileMovedTo) {
        if (!currentTile.containsStone() && !block.counterEffect(this)) {
        	Locator.getAudio().playSound(SOUND.FIRE.getValue());
            currentTurn.addCommand(new RemoveCommand(block, currentTile, gameBoard));
        }
    }

    @Override
    public Command retract() {
        return null;
    }
    
    @Override
	public void setInfo() {
		info="Name:  Fire Trap\n\nFunction:  Destroys all blocks except Stone Block.";
	}

}
